package vn.com.bookstore.adapter;

import java.util.ArrayList;

import vn.com.bookstore.Config;
import vn.com.bookstore.R;
import vn.com.bookstore.model.Book;
import vn.com.bookstore.model.Store;
import vn.com.bookstore.util.lazzyloading.ImageLoader;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StoreAdapter extends BaseAdapter{
	Context context;
	ArrayList<Store> stores;

	public StoreAdapter(Context context,ArrayList<Store> data) {
		this.context = context;
		this.stores = data;
	}
	
	public synchronized void setData(ArrayList<Store> data){
		this.stores = data;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return stores.size();
	}

	@Override
	public Object getItem(int position) {
		return stores.get(0);
	}

	@Override
	public long getItemId(int position) {
		return stores.get(0).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			BookHolder holder = new BookHolder(context);
			convertView = holder.parent;
			convertView.setTag(holder);
		}
		BookHolder holder = (BookHolder)convertView.getTag();
		holder.setData(stores.get(position));
		
		return holder.parent;
	}
	
	public class BookHolder{
		View parent;
		ImageView avatar;
		TextView title,sample;
		ImageView bookMark;
		
		public BookHolder(Context context) {
			parent = View.inflate(context, R.layout.item_store, null);
			avatar = (ImageView)parent.findViewById(R.id.avatar);
			title = (TextView)parent.findViewById(R.id.title);
			sample = (TextView)parent.findViewById(R.id.sample);
			bookMark = (ImageView)parent.findViewById(R.id.bookmark);
		}
		
		public void setData(Book book){
			title.setText(book.title);
			sample.setText(book.author);
		}
		
		public void setData(Store store){
			title.setText(store.getName());
			sample.setText(store.desc);
			ImageLoader loader = new ImageLoader(context);
			String urlImage = Config.getUrlImage(store.image);
			loader.DisplayImage(urlImage, avatar);
			bookMark.setVisibility(store.bookMark?View.VISIBLE:View.GONE);
		}
	}
}
